import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')
// import Login from './components/Login.vue'
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')
// import Home from './components/Home.vue'
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue')
// import Welcome from './components/Welcome.vue'
// eslint-disable-next-line no-unused-vars
const User = () => import(/* webpackChunkName:"user" */ '../components/user/User.vue')
// import Users from './components/user/Users.vue'
const Rights = () => import(/* webpackChunkName:"power" */ '../components/power/Rights.vue')
// import Rights from './components/power/Rights.vue'
const Roles = () => import(/* webpackChunkName:"power" */ '../components/roles/Roles.vue')
// import Roles from './components/power/Roles.vue'
const Cate = () => import(/* webpackChunkName:"goods" */ '../components/goods/Cate.vue')
// import Cate from './components/goods/Cate.vue'
const Params = () => import(/* webpackChunkName:"goods" */ '../components/goods/Params.vue')
// import Params from './components/goods/Params.vue'
// eslint-disable-next-line no-unused-vars
const List = () => import(/* webpackChunkName:"goods" */ '../components/goods/List.vue')
// import GoodList from './components/goods/List.vue'
// eslint-disable-next-line no-unused-vars
const Add = () => import(/* webpackChunkName:"goods" */ '../components/goods/Add.vue')
// import GoodAdd from './components/goods/Add.vue'
const Order = () => import(/* webpackChunkName:"order" */ '../components/order/Order.vue')
// import Order from './components/order/Order.vue'
const Report = () => import(/* webpackChunkName:"report" */ '../components/report/Report.vue')
// import Report from './components/report/Report.vue'
Vue.use(VueRouter)
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home',
    component: Home,
    redirect: '/welcome',
    children:[
      { path:'/welcome', component: Welcome },
      { path:'/users', component: User },
      { path:'/rights', component: Rights },
      { path:'/roles', component: Roles },
      { path:'/categories', component: Cate },
      { path:'/params', component: Params },
      { path:'/goods', component: List },
      { path:'/goods/add', component: Add },
      { path:'/orders', component: Order },
      { path:'/reports', component: Report }
    ] }
]

const router = new VueRouter({
  routes
})
// 使用路由导航守卫进行登录拦截
router.beforeEach((to, from, next) => {
  // to表示要跳转到哪里去,from表示从哪里来,next表示放行
// 先判断用户访问的是否是登录页面,如果是,直接放行
  if (to.path === '/login') return next()
  // 如果想要跳转到其他页面,那么判断有没有携带token
  let tokenStr = window.sessionStorage.getItem('token')
  // 如果验证不通过,那么强制跳转到登录页面
  if (!tokenStr) return next('/login')
  // 如果验证通过,那么直接放行
  next()
})

export default router
